package com.uc.browser.main;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import com.uc.browser.useragent.UserAgent;
import com.uc.browser.useragent.UserAgentFactory;
import com.uc.browser.useragent.XUCBrowserUserAgent;

/**
 * 
 * <b>GUITest简介:</b>
 * <p>UserAgent GUI测试程序</p>
 *
 * @author raorq
 * @version 1.0
 */
public class GUITest extends JFrame{
	
	
	//display the status
	private JLabel statusBar;

	// JTextAreas for displaying and inputting messages   
	private JTextArea resultArea;
	private JTextArea inputArea;  
	
	private Thread RThread;
	
	//Button for action
	private JButton jbtUserAgent;
	private JButton jbtXUA;
	private JButton jbtClear;
	
	public GUITest(){
		//resolve = new resolveAction();
		
		//define the input and result area size
		inputArea = new JTextArea(3,15);
		inputArea.setLineWrap(true);
		inputArea.setEditable(true);
		inputArea.setWrapStyleWord(true);
		
		resultArea = new JTextArea(100,100);
		resultArea.setLineWrap(true);
		resultArea.setEditable(false);
		resultArea.setWrapStyleWord(true);
		JScrollPane resultAreaScroll = new JScrollPane(resultArea); 
		//把定义的JTextArea放到JScrollPane里面去 
 
		
		//define the resolve and clear button
		jbtUserAgent = new JButton("parse User-Agent");
		jbtXUA = new JButton("parse X-UCBrowser-UA");
		jbtClear = new JButton("Clear");
		
		//define action listener
		ActionListener actResolve = new UAListener();
		jbtUserAgent.addActionListener(actResolve);
		ActionListener actResolve1 = new XUAListener();
		jbtXUA.addActionListener(actResolve1);
		ActionListener actClear = new ClearListener();
		jbtClear.addActionListener(actClear);
 
		
	 
		//define the panel to store the elements
		JPanel menu = new JPanel();
		menu.setLayout(new GridLayout(1,3,5,5));
		menu.add(jbtUserAgent);
		menu.add(jbtXUA);
		menu.add(jbtClear);
		
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout(5,5));
		top.add(inputArea,BorderLayout.CENTER);
		top.add(menu,BorderLayout.EAST);
		top.setBorder(new TitledBorder("Please input the data"));
		
		JPanel center = new JPanel();
		center.setLayout(new BorderLayout(5,5));
		center.add(resultAreaScroll,BorderLayout.CENTER);
		center.setBorder(new TitledBorder("Parse results:"));
		
		Container container = getContentPane();
		container.add(top, BorderLayout.NORTH);
		container.add(center,BorderLayout.CENTER);
 	
	}
	
	 
	private class XUAListener implements ActionListener{

        public void actionPerformed(ActionEvent arg0) {
            String text =  inputArea.getText();
            XUCBrowserUserAgent ua = UserAgentFactory.createXUCBrowserUserAgent(text);
            resultArea.setText(ua.toString());
            resultArea.setAutoscrolls(true);
           
        }
        
    }
	
	private class UAListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
		   String text =  inputArea.getText();
		   UserAgent ua = UserAgentFactory.createUserAgent(text);
		   resultArea.setText(ua.toString());
		   resultArea.setAutoscrolls(true);
		}
		
	}
	
	private class ClearListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			resultArea.setText("");
			inputArea.setText("");
			
		}
		
	}
	static boolean checkSOA(byte input[],String address) throws IOException{
		boolean isSOA=false;
		ByteArrayInputStream arr = new ByteArrayInputStream(input);
		DataInputStream in = new DataInputStream(arr);
		int number;
		
		//skip the header
		for(int i=0;i<12;i++)
			in.readByte();
		
		//skipe the question
		for(int i=0;i<address.length()+2;i++)
			in.readByte();
		in.readShort();
		in.readShort();
		number=in.readByte();
		//if it followed by a pointer
		if(number<0){
			in.readByte();
			number=in.readShort();
			if(number==6)
				return true;
		}
		//if it is nothing else
		else if(number==0){
			number=in.readShort();
			if(number==6)
				return true;
			else 
				return false;
		}
		else{
			return false;
			
		}
		
		
		return isSOA;
	}
	
	 
	
	//convert the String address into a byte array type
	static byte[] toRoot(String address) throws NumberFormatException, IOException{
		
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		DataOutputStream addr=new DataOutputStream(output);
		String split[]=address.split("\\.");
		
		for(int i=0;i<split.length;i++){
			addr.writeByte(Integer.parseInt(split[i]));
		}
		
		
		return output.toByteArray();
		
	}
	
	
	public static void main(String[] args){
		GUITest gui = new GUITest();
		gui.setVisible(true);
		gui.setResizable(true);
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//gui.setLocationRelativeTo(null);
		gui.setSize(1000,600);
	}

}